package action;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.ICity;
import model.IRouts;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.CityService;
import service.RoutsService;
import service.ServiceManager;
import form.SearchForm;

public class SearchAction extends ConnectionAwareAction {

	private static final Logger logger = Logger.getLogger(SearchAction.class);
	private static final String SEARCHACTION_INFO = "Responce is empty"; 
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)

	throws Exception {

		SearchForm input = (SearchForm) form;
		HttpSession session = request.getSession(true);
		try {
			int id_routs = input.getId_routs();
			String searchFrom = Escape.escapeHTML(input.getSearchFrom());
			String searchTo = Escape.escapeHTML(input.getSearchTo());
			String date_routs = Escape.escapeHTML(input.getDate_routs());
			if (searchFrom != null && searchTo != null) {
				session.setAttribute("searchFrom", searchFrom);
				session.setAttribute("searchTo", searchTo);				
				session.setAttribute("date_routs", date_routs);
			} 

			CityService cityService = ServiceManager.getDefault().getCityService();
			CityService cityService2 = ServiceManager.getDefault().getCityService();
			
			RoutsService routsService = ServiceManager.getDefault().getRoutsService();
			RoutsService routsService2 = ServiceManager.getDefault().getRoutsService();
			
			List<IRouts> searchs = null;
			ICity lcityFrom = null;
			ICity lcityTo = null;
			IRouts selectedRouts = null;
			if (searchFrom !=null && searchTo !=null){
	            lcityFrom = cityService.getCity(searchFrom);
	            lcityTo = cityService2.getCity(searchTo);
				searchs = routsService.getRoutsId(searchFrom, searchTo, date_routs);
			}
			if (!searchs.isEmpty() && lcityFrom !=null && lcityTo != null) {
				request.setAttribute("lcityFrom", lcityFrom);
				request.setAttribute("lcityTo", lcityTo);
				request.setAttribute("searchs", searchs);
				request.setAttribute("searchTo", searchTo);
				request.setAttribute("searchFrom", searchFrom);
				request.setAttribute("messages", "");
			} else {
				request.setAttribute("messages", "This route is not found or this route is not correct date");
			}
			if (id_routs > 0) {
				int cityFrom = Integer.parseInt(input.getCityFrom());
				int cityTo = Integer.parseInt(input.getCityTo());
				
				session.setAttribute("cityFrom", cityFrom);
				session.setAttribute("cityTo", cityTo);
				session.setAttribute("id_routs", id_routs);
	
				selectedRouts = routsService2.getRoutsById(id_routs);
				request.setAttribute("selectedRouts", selectedRouts);
				return mapping.findForward(ForwardActionTo.SUCCESS2);
			}
		} catch (Exception e) {
			logger.error(SearchAction.SEARCHACTION_INFO + e.getMessage());
		} 
		return mapping.findForward(ForwardActionTo.SUCCESS);
	}
}
